<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="rw.ac.kiosc.jdbc.school.orm.User" %>
<%@ page import="java.util.List" %>
<%@ page import="rw.ac.kiosc.jdbc.school.School" %>
<%@ page import="rw.ac.kiosc.jdbc.school.orm.User" %>
<%
        School school = new School();
        User user = null;
        if (session == null) {
            response.sendRedirect("login.htm");
        }
        String userIp = (String) session.getAttribute("userIp");
        int id = 0;
        String userId = (String) session.getAttribute("userId");
        if (userIp != null && userId != null) {
            try {
                id = Integer.parseInt(userId);
            } catch (Exception ex) {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
            if (userIp.equals(request.getRemoteAddr())) {
                user = school.getUserDAO().getUserById(id);
                if (user == null) {
                    session.invalidate();
                    response.sendRedirect("login.htm");
                }
            } else {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
        } else {
            session.invalidate();
            response.sendRedirect("login.htm");
        }

        if ("POST".equals(request.getMethod())) {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String name = request.getParameter("name");
            school.addUser(username, password, name);
        }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>List of Users</title>
    </head>
    <body>
        <%@ include file="menu.jsp" %>
        <h3>List of Users</h3>
        <table border="1">
            <tbody>
                <tr>
                    <td>User ID</td>
                    <td>Username</td>
                    <td>Password</td>
                    <td>Name</td>
                </tr>
                <%
        List<User> users = school.getUserDAO().getAllUsers();
        for (User u : users) {
                %>
                <tr>
                    <td><%=u.getUserId()%></td>
                    <td><%=u.getUsername()%></td>
                    <td>********</td>
                    <td><%=u.getName()%></td>
                </tr>
                <%
        }
                %>
                <tr>
                    <form method="post" action="users.htm">
                        <td><input type="submit" value="Add new"/></td>
                        <td><input type="text" name="username" id="username"/></td>
                        <td><input type="password" name="password" id="password"/></td>
                        <td><input type="text" name="name" id="name"/></td>
                    </form>
                </tr>
            </tbody>
        </table>
    </body>
</html>

